import 'package:demo1/demo/swiper_demo.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../model/post.dart';
import '../../demo/listview_demo.dart';
import '../../demo/hello_demo.dart';
import '../../demo/drawer_demo.dart';
import '../../demo/bottom_navigation_bar_demo.dart';
import '../../demo/basic_demo.dart';
import '../../demo/view_demo.dart';
import 'settings_page.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

// 1 实现 SingleTickerProviderStateMixin
class _HomePageState extends State<HomePage> {
  final List<BottomNavigationBarItem> bottomNavItems = [
    BottomNavigationBarItem(
      backgroundColor: Colors.blue,
      icon: Icon(Icons.home),
      label: '首页',
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.green,
      icon: Icon(Icons.message),
      label: '消息',
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.amber,
      icon: Icon(Icons.shopping_cart),
      label: '购物车',
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.red,
      icon: Icon(Icons.person),
      label: '个人中心',
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.red,
      icon: Icon(Icons.person),
      label: '个人中心',
    ),
  ];
  PageController _pageController;
  int currentIndex = 0;
  final List<Widget> pages = [
    // ListViewDemo(),
    // BasicDemo(),
    // SettingsPage(),
    // ViewDemo(),
    ListViewDemo(),
    CatePage(),
    SwiperDemo(),
    //MePage()
    SettingsPage(),
    ViewDemo(),
  ];

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: 0);
  }

  @override
  void dispose() {
    super.dispose();
    _pageController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('XXXXXX应用'),
        actions: [
          IconButton(
              icon: Icon(Icons.search),
              onPressed: () {
                print('search....');
              }),
          IconButton(
              icon: Icon(Icons.history),
              onPressed: () {
                print('history....');
              }),
        ],
        elevation: 4.0, // 阴影
      ),
      body: _buildBodyWidget(),
      drawer: DrawerDemo(),
      //bottomNavigationBar: BottomNavigationBarDemo(),
      bottomNavigationBar: BottomNavigationBar(
        items: bottomNavItems,
        currentIndex: currentIndex,
        type: BottomNavigationBarType.shifting,
        onTap: (index) {
          print('onTap');
          _pageController.jumpToPage(index);
        },
      ),
    );
  }

  Widget _buildBodyWidget() {
    return PageView.builder(
      controller: _pageController,
      itemCount: pages.length,
      physics: NeverScrollableScrollPhysics(),
      itemBuilder: (context, index) {
        return pages[index];
      },
      onPageChanged: (index) {
        setState(() {
          currentIndex = index;
        });
      },
    );
  }
}

class Mainpage extends StatefulWidget {
  @override
  _MainpageState createState() => _MainpageState();
}

class _MainpageState extends State<Mainpage>
    with AutomaticKeepAliveClientMixin {
  @protected
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      child: Column(
        children: <Widget>[Text("data")],
      ),
    );
  }
}

class CatePage extends StatefulWidget {
  @override
  _CatePageState createState() => _CatePageState();
}

class _CatePageState extends State<CatePage>
    with AutomaticKeepAliveClientMixin {
  int a = 0;

  @override
  bool get wantKeepAlive => true;
  String _batteryPercentage = 'Battery precentage';

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      child: Column(
        children: <Widget>[
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          Text("a$a"),
          RaisedButton(
            child: Text("点击"),
            onPressed: () {
              a++;
              setState(() {});
            },
          ),
          Text(
            _batteryPercentage,
            style: TextStyle(
              fontSize: 20.0,
            ),
          ),
          RaisedButton(
            child: Text("查询电量"),
            onPressed: _getBatteryInformation,
          ),
        ],
      ),
    );
  }

  @override
  void initState() {
    super.initState();
  }

  static const batteryChannel = const MethodChannel('battery');

  Future<void> _getBatteryInformation() async {
    String batteryPercentage;
    try {
      var result = await batteryChannel.invokeMethod('getBatteryLevel');
      batteryPercentage = 'Battery level at $result%';
    } on PlatformException catch (e) {
      batteryPercentage = "Failed to get battery level: '${e.message}'.";
    }

    setState(() {
      _batteryPercentage = batteryPercentage;
    });
  }
}

class ShoppingCart extends StatefulWidget {
  @override
  _ShoppingCartState createState() => _ShoppingCartState();
}

class _ShoppingCartState extends State<ShoppingCart>
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
        padding: EdgeInsets.all(10.0),
        margin: EdgeInsets.all(20.0),
        width: MediaQuery.of(context).size.width,
        height: 48.0,
        alignment: Alignment.center,
        child: Container(
          height: 150,
          child: Swiper(
            scrollDirection: Axis.horizontal,
            itemBuilder: _swiperBuilder,
            itemCount: 5,
            pagination: new SwiperPagination(
                builder: DotSwiperPaginationBuilder(
              color: Colors.black54,
              activeColor: Colors.white,
            )),
            control: new SwiperControl(),
            autoplay: true,
            onTap: (index) => print('点击了第$index个'),
          ),
        ));
  }

  Widget _swiperBuilder(BuildContext context, int index) {
    return (Image.network(
        "https://i0.hdslb.com/bfs/feed-admin/1fae7c89e9ce7ed8de3d0740afbc0db2080d76fd.jpg@880w_388h_1c_95q",
        fit: BoxFit.fitHeight));
  }

  @override
  bool get wantKeepAlive => true;
}

class MePage extends StatefulWidget {
  @override
  _MePageState createState() => _MePageState();
}

class _MePageState extends State<MePage> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
